package com.lauor.test.controller;

import com.lauor.test.entity.User;
import com.lauor.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/test")
//user demo, 分类例子见test目录下内容
public class UserRestCtrl {
    @Autowired
    private UserService userService;

    //查询多条数据,时间排序
    @GetMapping(path = "/user/list")
    public List<User> queryAll(Integer start, Integer pageSize){
        start = start == null ? 0 : start;
        pageSize = pageSize == null ? 10 : pageSize;
        return userService.queryAll(start, pageSize);
    }

    //根据名称查找多条数据
    @GetMapping(path = "/user/list/name")
    public List<User> queryByName(String name){
        if (name == null) name = "name";

        return userService.queryByName(name);
    }

    //新建数据
    @PostMapping(path = "/user")
    public int createUser(@RequestBody User user){
        return userService.createUser(user);
    }

    //更新数据
    @PutMapping(path = "/user")
    public int modifyUser(@RequestBody User user, Integer id){
        if (id == null) id = 1;
        return userService.updateUser(user, id);
    }

    //删除数据
    @DeleteMapping(path = "/user")
    public int deleteUser(Integer id){
        if (id == null) id = 1;
        return userService.deleteUserById(id);
    }
}